package com.orchard.orchardfarmmanagementsystem.controller;

import com.orchard.orchardfarmmanagementsystem.dto.MaintenanceRecordsDTO;
import com.orchard.orchardfarmmanagementsystem.service.MaintenanceService;
import com.orchard.orchardfarmmanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api")
public class MaintenanceController {

    @Autowired
    private MaintenanceService maintenanceService;


    @GetMapping("/{materialId}/maintenance-records")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<MaintenanceRecordsDTO>> getMaintenanceRecords(@PathVariable Integer materialId){
        try{
            List<MaintenanceRecordsDTO> maintenanceRecords = maintenanceService.getMaintenanceRecords(materialId);
            return Result.success("获取维修记录信息成功",maintenanceRecords);
        }catch (Exception e){
            return Result.error(50001,"获取维修记录信息失败");

        }
    }

    @PostMapping("/maintenance")
    @PreAuthorize("hasAuthority('PERM_ADMIN_DASHBOARD')")
    public Result<MaintenanceRecordsDTO> createMaintenanceRecords(@RequestBody MaintenanceRecordsDTO maintenanceRecordsDTO){
        try{
            MaintenanceRecordsDTO maintenanceRecord = maintenanceService.createMaintenanceRecords(maintenanceRecordsDTO);
            return Result.success("新增维修记录信息成功",maintenanceRecord);
        }catch (Exception e){
            return Result.error(50001,e.getMessage());

        }
    }

    // 获取所有维修记录
    @GetMapping("/maintenance-records/all")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<MaintenanceRecordsDTO>> getAllMaintenanceRecords(){
        try{
            List<MaintenanceRecordsDTO> maintenanceRecords = maintenanceService.getAllMaintenanceRecords();
            return Result.success("获取所有维修记录信息成功", maintenanceRecords);
        } catch (Exception e){
            return Result.error(50001, "获取所有维修记录信息失败");
        }
    }

    // 完成（更新）特定的维修记录
    @PutMapping("/maintenance-records/{id}/complete")
    @PreAuthorize("hasAuthority('PERM_ADMIN_DASHBOARD')")
    public Result<MaintenanceRecordsDTO> completeMaintenanceRecord(@PathVariable Integer id, @RequestBody MaintenanceRecordsDTO maintenanceRecordsDTO){
        try{
            MaintenanceRecordsDTO updatedRecord = maintenanceService.completeMaintenanceRecord(id, maintenanceRecordsDTO);
            return Result.success("更新维修记录信息成功", updatedRecord);
        } catch (Exception e){
            return Result.error(50001, e.getMessage());
        }
    }

    @DeleteMapping("/maintenance-records/{id}")
    @PreAuthorize("hasAuthority('PERM_ADMIN_DASHBOARD')")
    public Result deleteMaintenanceRecord(@PathVariable Integer id) {
        try {
            maintenanceService.deleteMaintenanceRecord(id);
            return Result.success("删除维修记录成功");
        } catch (Exception e) {
            return Result.error(50001, e.getMessage());
        }
    }


}
